<html>
<head><meta charset="utf-8"><title>Generic arch with specialized code · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html">Generic arch with specialized code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204069563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204069563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204069563">(Jul 16 2020 at 11:02)</a>:</h4>
<p>Hello! I have a crate that i would like to work on x86_64 but with specialized code for avx2. The AVX2 code is not just a simple function that I can annotate with <code>#[target_feature]</code> but rather a whole module. In the generic part of the crate i will rely on runtime feature cpu detection to call the appropriate code.<br>
What is the right way of doing this?</p>



<a name="204070035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204070035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204070035">(Jul 16 2020 at 11:09)</a>:</h4>
<p>I cannot really rely on conditional compilation since i want to avx2 code to always be compiled  and part of the library without the user having to enable rustflags or anything</p>



<a name="204078087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204078087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204078087">(Jul 16 2020 at 12:51)</a>:</h4>
<p>write a proc macro to annotate every function in the module with <code>#[target_feature]</code>?</p>



<a name="204078110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204078110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204078110">(Jul 16 2020 at 12:51)</a>:</h4>
<p>if one doesn't already exist</p>



<a name="204080558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204080558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204080558">(Jul 16 2020 at 13:11)</a>:</h4>
<p>Using <code>#[target_feature]</code> also inhibits some inlining which can lead to (very) suboptimal codegen</p>



<a name="204080739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204080739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204080739">(Jul 16 2020 at 13:13)</a>:</h4>
<p>I was wondering if using a <code>build.rs</code> to manually call <code>cargo rustc</code> with the proper flags and then linking with the generated rlib would do the trick</p>



<a name="204080746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204080746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204080746">(Jul 16 2020 at 13:13)</a>:</h4>
<p>Only when calling from non-target_feature to target_feature</p>



<a name="204080774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204080774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204080774">(Jul 16 2020 at 13:13)</a>:</h4>
<p>And that should only happen once after runtime detection</p>



<a name="204081580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204081580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204081580">(Jul 16 2020 at 13:18)</a>:</h4>
<p>Hum ok</p>



<a name="204088794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204088794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204088794">(Jul 16 2020 at 14:13)</a>:</h4>
<p>yeah, it only prevents what would lead to UB due to unsupported instructions being generated</p>



<a name="204100059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204100059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204100059">(Jul 16 2020 at 15:30)</a>:</h4>
<p>You could also potentially do what I do in jetscii <a href="https://github.com/shepmaster/jetscii/blob/master/src/lib.rs">https://github.com/shepmaster/jetscii/blob/master/src/lib.rs</a></p>



<a name="204100117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204100117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204100117">(Jul 16 2020 at 15:31)</a>:</h4>
<p>See the dispatch macro</p>



<a name="204100753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Generic%20arch%20with%20specialized%20code/near/204100753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Generic.20arch.20with.20specialized.20code.html#204100753">(Jul 16 2020 at 15:34)</a>:</h4>
<p><a href="https://docs.rs/multiversion/">https://docs.rs/multiversion/</a> might do what you want <span class="user-mention" data-user-id="281572">@marmeladema</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>